﻿package com.bizmlib.utils.set
{
	public class SetManagerSimpleList extends SetManager
	{
		
		public function SetManagerSimpleList(nodeName:String = undefined)
		{
			super();
			itemNodeName = nodeName || itemNodeName;
		}
		
		
		public function parseXML(newXML:XML, oldXML:XML):void
		{
			//trace(this + ".parseXML(" + arguments + ")");
			
			var oldItemsList:Array = siftOutNodes(oldXML);
			var newItemsList:Array = siftOutNodes(newXML);
			
			
			for(var i:Number=0; i<newItemsList.length; i++)
			{
				var newItemXML:XML = newItemsList[i] as XML;
				
				if(newItem.localName()==itemNodeName)
				{
					var localWasFound:Boolean = false;
					
					for(var j:Number=0; j<oldItemsList.length; j++)
					{
						var oldItemXML:XML = oldItemsList[j] as XML;
						
						if(oldItemXML.attribute(keyAttribute)==newItemXML.attribute(keyAttribute))
						{
							localWasFound = true;
							dispatchEvent(new SetManagerEvent(SetManagerEvent.ITEM_UPDATE, i, undefined, newItemXML));
							oldItemsList.splice(j, 1);
							break;
						}
					}
					if(!localWasFound)
					{
						dispatchEvent(new SetManagerEvent(SetManagerEvent.ITEM_ADD, undefined, i, newItemXML));
					}
				}
			}
			for(var j:Number=0; j<oldItemsList.length; j++)
			{
				dispatchEvent(new SetManagerEvent(SetManagerEvent.ITEM_REMOVE, j, undefined, oldItemXML));
			}
		}
	}
}